package top.xiaoshuaila.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.xiaoshuaila.pojo.Admin;
import top.xiaoshuaila.pojo.AdminLoginParam;
import top.xiaoshuaila.pojo.RespBean;
import top.xiaoshuaila.service.IAdminService;
import top.xiaoshuaila.service.IRoleService;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@RestController
@Api(tags = "登录接口管理controller")
public class LoginController {

    @Autowired
    private IAdminService adminService;
    @Autowired
    private IRoleService roleService;
    @PostMapping("/login")
    @ApiOperation("登录接口")
    public RespBean login(@RequestBody AdminLoginParam adminLoginParam,
                          HttpServletRequest request){
        System.out.println(request.getHeader("tokenStr"));
        return adminService.login(adminLoginParam.getUsername(),
                adminLoginParam.getPassword(),adminLoginParam.getCode(),
                request);
    }
    @GetMapping("/admin/info")
    @ApiOperation("查看当前登录信息")
    public Admin getAdminInfo(Principal principal){
        if(null==principal)
        {
            return null;
        }
        String username=principal.getName();
        Admin admin=adminService.getAdminByUserName(username);
        admin.setPassword(null);
        admin.setRoles(adminService.getRoles(admin.getId()));
        return admin;

    }
    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public RespBean logout(){
        return RespBean.success("退出成功!");
    }
}
